/*
 * Copyright 2006-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ziroom.crm.mq.core.retry;

import java.io.Serializable;

/**
 * A {@link RetryPolicy} is responsible for allocating and managing resources
 * needed by {@link RetryOperations}. The {@link RetryPolicy} allows retry
 * operations to be aware of their context. Context can be internal to the retry
 * framework, e.g. to support nested retries. Context can also be external, and
 * the {@link RetryPolicy} provides a uniform API for a range of different
 * platforms for the external context.
 *
 * @author Dave Syer
 */
@SuppressWarnings("ALL")
public interface RetryPolicy extends Serializable {

    /**
     * 能否重试
     *
     * @param retryContext 重试上下文
     * @return true if the operation can proceed
     */
    boolean canRetry(RetryContext retryContext);

    /**
     * Acquire resources needed for the retry operation. The callback is passed
     * in so that marker interfaces can be used and a manager can collaborate
     * with the callback to set up some state in the status token.
     *
     * @param parent the parent context if we are in a nested retry.
     * @return a {@link RetryContext} object specific to this policy.
     */
    RetryContext open(RetryContext parent);

    /**
     * 执行失败逻辑
     * 通过该方法来执行重试失败后的处理逻辑。
     * 方法不做是否可执行的判断（即是否跟进exception 是否存在来判断是否执行），这层逻辑交于调用者判断
     * 方法仅关注重试失败后逻辑的执行，只要调用了，就认为有必要找执行失败逻辑。
     *
     * @param context   重试内容
     * @param exception 异常体。可空
     */
    void dealException(RetryContext context, Exception exception);

}
